package algorithm.kruskal;

import java.util.Arrays;

public class KruskalCase {

  private int edgeNum; //边的个数
  private char[] vertexs;//顶点数组
  private int[][] matrix;//领接矩阵
  private static final int INF = Integer.MAX_VALUE;//使用INF表示两个顶点不能连通

  public static void main(String[] args) {

    char[] vertexs = {'A','B','C','D','E','F','G'};
    int[][]matrix = {
            {0,12,INF,INF,INF,16,14},
            {12,0,10,INF,INF,7,INF},
            {INF,10,0,3,5,6,INF},
            {INF,INF,3,0,4,INF,INF},
            {INF,INF,5,4,0,2,8},
            {16,7,6,INF,2,0,9},
            {14,INF,INF,INF,8,9,0},

    };

    KruskalCase kruskalCase = new KruskalCase(vertexs, matrix);
    kruskalCase.print();

    EData[] edges = kruskalCase.getEdges();
    System.out.println("未排序：" + Arrays.toString(edges));

    kruskalCase.sortEdge(edges);

    System.out.println("已排序：" + Arrays.toString(edges));

    kruskalCase.kruskal();

  }

  public KruskalCase(char[] vertexs,int[][]matrix){

//    初始化顶点数和边的个数
    int vlen = vertexs.length;
//    初始化顶点
    this.vertexs = new char[vlen];
    for (int i = 0; i < vlen; i++) {
      this.vertexs[i] = vertexs[i];
    }
//    初始化边
    this.matrix = new int[vlen][vlen];
    for (int i = 0; i < vlen; i++) {
      for (int j = 0; j < vlen; j++) {
        this.matrix[i][j] = matrix[i][j];
      }
    }
//    统计边
    for (int i = 0; i < vlen; i++) {
      for (int j = i + 1; j < vlen; j++) {
        if (this.matrix[i][j] != INF){
          edgeNum++;
        }
      }
    }
  }

//  打印矩阵
  public void print(){
    for (int i = 0; i < vertexs.length; i++) {
      for (int j = 0; j < vertexs.length; j++) {
        System.out.printf("%12d\t",matrix[i][j]);
      }
      System.out.println();
    }
  }

//  对边进行排序
  public void sortEdge(EData[] eData){
    for (int i = 0; i < eData.length - 1; i++) {
      for (int j = 0; j < eData.length - i - 1; j++) {
        if (eData[j].weight > eData[j + 1].weight){
          EData temp = eData[j];
          eData[j] = eData[j + 1];
          eData[j + 1] = temp;
        }
      }
    }
  }
//  返回顶点下标的方法
  public int getPosition(char ch){
    for (int i = 0; i < vertexs.length; i++) {
      if (vertexs[i] == ch){
        return i;
      }
    }
    return -1;
  }
//  获得图中的边的方法，形式['A','B','12']
  public EData[] getEdges(){

    EData[] eData = new EData[edgeNum];
    int index = 0;
    for (int i = 0; i < vertexs.length; i++) {
      for (int j = i + 1; j < vertexs.length; j++) {
        if (matrix[i][j] != INF){
          eData[index++] = new EData(vertexs[i],vertexs[j],matrix[i][j]);
        }
      }
    }
    return eData;
  }
//  根据顶点的下标，获取其终点的下标
  /*
  ends：数组就是记录了各个顶点对应的终点是哪个,ends数组是在遍历过程中，逐步变化的
  i：表示传入的顶点对应的下标
  return：返回的就是下标为i的这个顶点对应的终点的下标
   */
  public int getEnd(int[]ends,int i){
    while (ends[i] != 0){
      i = ends[i];
    }
    return i;
  }
//  kruskal算法
  public void kruskal(){
    int index = 0;//结果数组的下标

    int[]ends = new int[edgeNum];

    ///创建结果数组,保存最后的最小生成树
    EData[] res = new EData[edgeNum];

    //共12条边
    EData[] edges = getEdges();

//    从小到大排序
    sortEdge(edges);

//    遍历
    for (int i = 0; i < edges.length; i++) {
//      获取顶点的起点
      int p1 = getPosition(edges[i].start);//p1=4
//      获取顶点的终点
      int p2 = getPosition(edges[i].end);//p2=5
//      p1这个顶点在已有最小生成树中的终点
      int end1 = getEnd(ends, p1);//end1=4
//      p2这个顶点在已有最小生成树中的终点
      int end2 = getEnd(ends, p2);//end2=5
      if (end1 != end2){//没有构成环路
        ends[end1] = end2;//假设此时是<E,F>,ends[0,0,0,0,5,0,0,0,0,0,0,0]
        res[index++] = edges[i];//将边加到结果集中
      }
    }
    System.out.println("打印最小生成树：" );
    for (int i = 0; i < index; i++) {
      System.out.print(res[i] + " ");
    }
  }
}
//边的类
class EData{
  int weight;
  char start;
  char end;

  public EData(char start, char end,int weight) {
    this.weight = weight;
    this.start = start;
    this.end = end;
  }

  @Override
  public String toString() {
    return "EData[" +
            "<" + start +
            "," + end +
            ">=" + weight +
            ']';
  }
}